﻿@using Church.Common
@model Church.Models.Shared.PagerModel

@{
	Layout = null;
}

<div class="pagination">
	@if (Model != null)
	{
		<ul id="pagination-digg">
			@{
				//Prev Page
				<li class="previous">
					<a href="@Constants.EMPTY_ANCHOR_LINK" data-page="@(Model.CurrentPage - 1)" data-type="@(Model.CurrentPage <= 1 ? "disabled" : string.Empty)"></a>
				</li>

				//First Page
				<li @Html.Raw(Model.CurrentPage == 1 ? "class='active'" : string.Empty)>
					<a href="@Constants.EMPTY_ANCHOR_LINK" data-page="1" data-type="@(Model.CurrentPage == 1 ? "disabled" : string.Empty)">1</a>
				</li>

				//First ...
				@Html.Raw(Model.CurrentPage + Constants.COUNT_PAGES_BOTH_SIDE_CURR_PAGE >= Constants.MAX_COUNT_VISIBLE_PAGES && Model.CountPages > Constants.MAX_COUNT_VISIBLE_PAGES ? "<li><a>...</a></li>" : string.Empty)

				//Pages
				if (Model.CountPages > 2)
				{
					var pageNumber = Model.CurrentPage - Constants.COUNT_PAGES_BOTH_SIDE_CURR_PAGE <= 1 || Model.CountPages <= Constants.MAX_COUNT_VISIBLE_PAGES
						? 2
						: Model.CountPages - Constants.COUNT_PAGES_BOTH_SIDE_CURR_PAGE <= Model.CurrentPage
							? Model.CountPages - Constants.COUNT_PAGES_BOTH_SIDE_CURR_PAGE * 2 - 1
							: Model.CurrentPage - Constants.COUNT_PAGES_BOTH_SIDE_CURR_PAGE;

					for (int i = 0; i < Constants.COUNT_PAGES_BOTH_SIDE_CURR_PAGE * 2 + 1 && pageNumber < Model.CountPages; i++)
					{
						<li @Html.Raw(Model.CurrentPage == pageNumber ? "class='active'" : string.Empty)>
							<a href="@Constants.EMPTY_ANCHOR_LINK" data-page="@pageNumber" data-type="@(Model.CurrentPage == pageNumber ? "disabled" : string.Empty)">@pageNumber</a>
						</li>
						pageNumber++;
					}
				}

				//Last ...
				@Html.Raw(Model.CurrentPage + Constants.COUNT_PAGES_BOTH_SIDE_CURR_PAGE + 1 < Model.CountPages && Model.CountPages > Constants.MAX_COUNT_VISIBLE_PAGES ? "<li><a>...</a></li>" : string.Empty)

				//Last Page
				if (Model.CountPages != 1)
				{
					<li @Html.Raw(Model.CurrentPage == Model.CountPages ? "class='active'" : string.Empty)>
						<a href="@Constants.EMPTY_ANCHOR_LINK" data-page="@Model.CountPages" data-type="@(Model.CurrentPage == Model.CountPages ? "disabled" : string.Empty)">@Model.CountPages</a>
					</li>
				}

				//Next Page
				<li class="next">
					<a href="@Constants.EMPTY_ANCHOR_LINK" data-page="@(Model.CurrentPage + 1)" data-type="@(Model.CurrentPage >= Model.CountPages ? "disabled" : string.Empty)"></a>
				</li>
			}
		</ul>
	}
</div>
